// pages/songs/songs.js
const utils = require('../../utils/util.js');

Page({
  data: {
    RoomList: [],
    PlayList: []
  },

  onLoad(options) {
    this.getAllRooms();
  },

  onShow() {
    this.getAllRooms();
  },

  getAllRooms() {
    wx.request({
      url: 'http://localhost:3000/GetAllRooms', 
      method: 'GET',
      header: {  
        'content-type': 'application/json', // 根据需要设置  
        'Authorization': wx.getStorageSync('usertoken')
      },  
      success: (res) => {
        
        
        if (res.data.code === 210 && Array.isArray(res.data.data)) {
          const rooms = res.data.data.map(room => ({
            roomId: room.Id,
            roomName: room.roomName,
            roomDiscription: room.roomDiscribe,
            roomPassword: room.roomPassword
          }));
          wx.setStorageSync('RoomList', rooms);
          this.setData({
            RoomList: rooms
          });
        }  else if(res.data.code === 401){
            utils.userlogin();
        }
      },
      fail: () => {
        wx.showToast({
          title: '请求失败',
          icon: 'none'
        });
      }
    });
  },

  Enter_Room: function (e) {
    const roomId = parseInt(e.currentTarget.dataset.roomid); // 获取点击的按钮的房间编号
    const roomData = this.data.RoomList.find(room => room.roomId === roomId);
    const data = e.currentTarget.dataset.data;

    if (roomData) {
      // 将数据传递给下一个页面
      wx.reLaunch({
        url: `/pages/room/room?roomId=${roomData.roomId}&roomName=${roomData.roomName}&roomDiscription=${roomData.roomDiscription}`
      });
    } else {
      console.error('Room not found for roomId:', roomId);
    }
  },

  To_Create_Room: function () {
    utils.To_Create_Room();
  },

})
